package com.starview.star.compiler.wff;

import com.starview.platform.data.type.Location;
import com.starview.star.compiler.ast.IAbstract;
import com.starview.star.compiler.util.PrettyPrintDisplay;
import com.starview.star.compiler.wff.WffOp.applyMode;

@SuppressWarnings("serial")
public class WffWhere implements WffCond
{
  private final WffCond tgt;
  private final Location loc;
  private final WffProgram subRules;

  public WffWhere(Location loc, WffCond tgt, WffProgram rules)
  {
    this.loc = loc;
    this.tgt = tgt;
    this.subRules = rules;
  }

  @Override
  public applyMode satisfied(IAbstract[] env, Location loc, WffEngine engine)
  {
    int mark = engine.pushRules(subRules);
    applyMode satisfied = tgt.satisfied(env, loc, engine);
    engine.reset(mark);
    return satisfied;
  }

  public Location getLoc()
  {
    return loc;
  }

  @Override
  public void prettyPrint(PrettyPrintDisplay disp)
  {
    tgt.prettyPrint(disp);
    disp.append("##");
    int mark = disp.markIndent(2);
    subRules.prettyPrint(disp);
    disp.popIndent(mark);
  }
}
